package com.sdau.repairmaster.UIFile;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.sdau.repairmaster.R;

import org.w3c.dom.Text;

public class ShowDeairRepairInfo extends AppCompatActivity {
    private String address;
    private String type;
    private String describe;
    private String path;
    private String flag;


    private TextView showAdress;
    private TextView showType;
    private TextView showDescribe;
    private ImageView showRepairPhoto;
    private TextView showState;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_deair_repair_info);
        initView();
    }

    public void initView(){
        showAdress = (TextView) findViewById(R.id.show_ev_repair_address);
        //showAdress.setMovementMethod(ScrollingMovementMethod.getInstance());
        showType = (TextView) findViewById(R.id.show_ev_repair_type);
        showDescribe = (TextView) findViewById(R.id.show_et_repair_describe);
        showRepairPhoto = (ImageView) findViewById(R.id.show_iv_repair_Picture);
        showState = (TextView) findViewById(R.id.show_et_repair_state);

        address = getIntent().getStringExtra("address");
        type = getIntent().getStringExtra("type");
        describe = getIntent().getStringExtra("describe");
        path = getIntent().getStringExtra("path");
        flag = getIntent().getStringExtra("flag");

        showAdress.setText(address);
        showType.setText(type);
        showDescribe.setText(describe);
        if(flag!=null && "0".equals(flag.trim())) showState.setText("未处理");
        else showState.setText("已处理");

        path = "http://10.0.2.2:8080/RepairAdmin/" + path;

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)//是否启用内存缓存
                .cacheOnDisk(true)//是否启用磁盘缓存
                //可以控制图片展示的样式（形式）
                .displayer(new BitmapDisplayer() {
                    @Override
                    public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
                        Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
                        Canvas canvas = new Canvas(bitmap1);
                        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                        Paint paint = new Paint();
                        paint.setShader(shader);
                        //这里绘制了一个圆形
                        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getHeight() / 2, paint);
                        imageAware.setImageBitmap(bitmap1);
                    }
                }).build();

        //构建者..需要一个上下文
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
                .memoryCacheSizePercentage(20)//设置占用内存的百分比（二级缓存
                .diskCacheFileCount(100)//磁盘缓存
                .diskCacheSize(5 * 1024 * 1024)
                //  .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                //展示图片的一个状态
                .defaultDisplayImageOptions(options)
                .build();

        //构建者必须要有
        ImageLoader.getInstance().init(configuration);

        //以圆形
        //ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(this));
        //ImageLoader.getInstance().displayImage(url,imageView);

        //同理，下面的option为五角星样式
        DisplayImageOptions options1 = new DisplayImageOptions.Builder().displayer(
                new BitmapDisplayer() {
                    @Override
                    public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
                        Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
                        Canvas canvas = new Canvas(bitmap1);
                        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                        Paint paint = new Paint();
                        paint.setShader(shader);
                        //graphics包下的...
                        Path path = new Path();
                        path.moveTo(bitmap.getWidth() / 2, 0);
                        path.lineTo(bitmap.getWidth() / 4, bitmap.getHeight());
                        path.lineTo(bitmap.getWidth(), bitmap.getHeight() / 3);
                        path.lineTo(0, bitmap.getHeight() / 3);
                        path.lineTo(bitmap.getWidth() / 4 * 3, bitmap.getHeight());
                        canvas.drawPath(path, paint);
                        imageAware.setImageBitmap(bitmap1);
                    }
                }
        ).build();

        //ImageLoader.getInstance().loadImage();//不展示，仅仅下载一张图片
        //以五角星形

        //加载图片用
        ImageLoader.getInstance().displayImage(path, showRepairPhoto, options);

        //下载，提供两个监听ImageLoadingListener、ImageLoadingProgressListener
        ImageLoader.getInstance().displayImage(path, showRepairPhoto, options, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }
            //当前进度，如果想要写一个进度条一类的东西，可以在这里
        }, new ImageLoadingProgressListener() {
            @Override
            public void onProgressUpdate(String imageUri, View view, int current, int total) {

            }
        });
    }
}